<?php 
ini_set ( 'date.timezone', 'Asia/Shanghai' );
define ( 'FC_PATH', dirname ( __FILE__ ) . DIRECTORY_SEPARATOR );
define ( 'BASEPATH', FC_PATH . 'system' );
class Update{
	function __construct() {
		
	}

	//执行sql更新
	function exutesql(){
		header("Content-type: text/html; charset=utf-8");  
		include FC_PATH . 'application' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
		$dbconfig = $db ['default'];
		define ( 'DB_TABLEPRE', $dbconfig['dbprefix'] );
		$tablepre=$dbconfig['dbprefix'];
	
		$con=mysqli_connect($dbconfig['hostname'], $dbconfig['username'], $dbconfig['password']);
		
		mysqli_select_db($con,$dbconfig['database']);
		
		$sql_class1 = "alter table " .DB_TABLEPRE. "usergroup add COLUMN canfreereadansser int(10) DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新usergroup表，增加canfreereadansser字段<br>';
	
		
		//新增用户通知表
		
		$sql="CREATE TABLE IF NOT EXISTS `" . DB_TABLEPRE . "user_notify` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL COMMENT '用户uid',
  `inbox_permission` int(2) DEFAULT 0 COMMENT '0 全部站内用户 1 关注我的',
  `invite_permission` int(2) DEFAULT 0 COMMENT '0所有人 1关注我的',
  `follow_after_answer` int(2) DEFAULT 1 COMMENT '1自动关注 0 不关注',
    `article` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
    `like_object` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `bookmark_object` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `follow_object` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `answer` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `comment` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `content_handled` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `comment_reply` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `invite` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `message` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `weekly` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
 `feature_news` int(2) DEFAULT 1 COMMENT '1通知 0不通知',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户通知表';
		";
		if(mysqli_query($con,$sql )){
			echo ' 更新成功:新增user_notify用户通知表<br>';
		}
		
		
		//增加问题表全文检索同步索引
		$sql_class1 = "alter table " . DB_TABLEPRE . "question add COLUMN isupdatexunsearch int(10)  DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "question`  ADD  INDEX isupdatexunsearch (  `isupdatexunsearch`  )";
		mysqli_query($con, $sql );
		echo "question表增加isupdatexunsearch字段-xunsearch全文检索是否同步索引<br>";
		
		//增加文章表全文检索同步索引
		$sql_class1 = "alter table " . DB_TABLEPRE . "topic add COLUMN isupdatexunsearch int(10)  DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX isupdatexunsearch (  `isupdatexunsearch`  )";
		mysqli_query($con, $sql );
		echo "topic表增加isupdatexunsearch字段-xunsearch全文检索是否同步索引<br>";
		
		//---问答分类增加author--作者
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "category add COLUMN author  varchar(20) DEFAULT '';";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新category表，增加author 作者<br>';
		
		//---问答分类增加authorid--作者id
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "category add COLUMN authorid  mediumint(9) DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新category表，增加authorid 作者id<br>';
		
		

		
	
		
		$sql_class1 = "alter table " . DB_TABLEPRE . "category add COLUMN onlybackground int(2) DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX onlybackground (  `onlybackground`  )";
		mysqli_query($con, $sql );


		
		//---问答分类增加iscourse--是否是课程分类
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "category add COLUMN iscourse int(2)  DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新category表，增加iscourse是否是课程分类<br>';
		
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX iscourse (  `iscourse`  )";
		mysqli_query($con, $sql );
		
		//---问答分类增加onlybackground--是否只在后台发布分类
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "category add COLUMN onlybackground int(2)  DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新category表，增加onlybackground是否只在后台发布分类<br>';
		
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX onlybackground (  `onlybackground`  )";
		mysqli_query($con, $sql );
		//---微信开放平台登录wechatopenid
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN wechatopenid VARCHAR(200)  DEFAULT '';";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加wechatopenid邀请码字段<br>';
		//---真实姓名
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN conpanyname VARCHAR(100)  DEFAULT '';";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加conpanyname公司名称字段<br>';
		
		//---真实姓名
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN truename VARCHAR(50)  DEFAULT '';";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加truename真实姓名字段<br>';
		//---用户邀请码
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN invatecode VARCHAR(200)  DEFAULT NULL;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加invatecode邀请码字段<br>';
		//---邀请人的邀请码
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN frominvatecode VARCHAR(200)  DEFAULT NULL;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加frominvatecode邀请码字段<br>';
		//---用户邀请人数
		//-----
		$sql_class1 = "alter table " . DB_TABLEPRE . "user add COLUMN invateusers int(10)  DEFAULT 0;";
		mysqli_query($con, $sql_class1 );
		echo ' 更新成功:更新user表，增加invateusers邀请码字段<br>';
		
		
		//更新用户表--增加registrationid字段
		$sql= "alter table " .DB_TABLEPRE. "user add COLUMN registrationid varchar(200)  DEFAULT null;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新用户表，增加registrationid极光设备号id<br>';
		//更新文章--增加发布状态字段
		$sql= "alter table " .DB_TABLEPRE. "topic add COLUMN yuyin int(5)  DEFAULT 0;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章表，增加yuyin是否存在语音文件，默认0，0表示没有<br>';
		//更新文章--增加发布是否是课程字段
		$sql= "alter table " .DB_TABLEPRE. "topic add COLUMN iscourse int(5)  DEFAULT 0;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章表，增加iscourse是课程类型，默认0，0表示不是<br>';
		//更新文章--增加发布状态字段
		$sql= "alter table " .DB_TABLEPRE. "topic add COLUMN state int(5)  DEFAULT 1;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章表，增加state发布状态，默认直接发布1，0表示审核<br>';
		//更新文章评论--增加发布状态字段
		$sql = "alter table " .DB_TABLEPRE. "article_comment add COLUMN state int(5)  DEFAULT 1;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章评论表，增加state发布状态，默认直接发布1，0表示审核<br>';
		//更新文章评论回复表--增加发布状态字段
		$sql = "alter table " .DB_TABLEPRE. "articlecomment add COLUMN state int(5)  DEFAULT 1;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章评论回复表，增加state发布状态，默认直接发布1，0表示审核<br>';
		//增加文章阅读模式
		$sql = "alter table " .DB_TABLEPRE. "topic add COLUMN readmode int(5)  DEFAULT 1;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章表，增加readmode阅读模式，1免费阅读 ，2积分阅读，3 付费阅读<br>';
		//更新文章带积分的
		$sql = "update  `" . DB_TABLEPRE . "topic` SET `readmode` = '2' WHERE price>0 and readmode!=3";
		mysqli_query($con, $sql );
		//设置模板为default
		mysqli_query($con, "delete from `". DB_TABLEPRE . "setting` where `k`='tpl_dir'" );
		
		mysqli_query($con, "INSERT INTO `".DB_TABLEPRE."setting` SET `k`='tpl_dir',`v`='default'" );

		//设置模板为fronzewap
		mysqli_query($con, "delete from `". DB_TABLEPRE . "setting` where `k`='tpl_wapdir'" );

		mysqli_query($con, "INSERT INTO `".DB_TABLEPRE."setting` SET `k`='tpl_wapdir',`v`='fronzewap'" );
		echo ' 更新成功:更新设置表，重置模板PC模板<br>';
		
		//增加付费阅读简介
		$sql = "alter table " .DB_TABLEPRE. "topic add COLUMN freeconent varchar(500)  DEFAULT null;";
		mysqli_query($con, $sql );
		echo ' 更新成功:更新文章表，增加freeconent试看内容<br>';
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "tag_item`  ADD  INDEX typeid (  `typeid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "tag_item`  ADD  INDEX itemtype (  `itemtype`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "tag`  ADD  INDEX tagalias (  `tagalias`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "tag`  ADD  INDEX tagname (  `tagname`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX index (  `state`,  `yuyin`,  `readmode`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX state (  `state`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX yuyin (  `yuyin`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX readmode (  `readmode`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "articlecomment`  ADD  INDEX state (  `state`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "article_comment`  ADD  INDEX state (  `state`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "answer`  ADD  INDEX status (  `status`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "question`  ADD  INDEX status (  `status`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  ADD  INDEX openid (  `openid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  ADD  INDEX wechatopenid (  `wechatopenid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  ADD  INDEX mypay (  `mypay`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  ADD  INDEX active (  `active`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  ADD  INDEX hasvertify (  `hasvertify`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "doing`  ADD  INDEX questionid (  `questionid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "doing`  ADD  INDEX action (  `action`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  ADD  INDEX iscourse (  `iscourse`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX pid (  `pid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX grade  (  `grade`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX questions (  `questions`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX followers (  `followers`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "category`  ADD  INDEX authorid (  `authorid`  )";
		mysqli_query($con, $sql );
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "user`  modify introduction varchar(500)";
		mysqli_query($con, $sql );
		
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "question`  modify title varchar(200)";
		mysqli_query($con, $sql );
		
		$sql="ALTER  TABLE  `" . DB_TABLEPRE . "topic`  modify describtion mediumtext";
		mysqli_query($con, $sql );
		
	}
}
$update=new Update();
$update->exutesql();
?>